<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2019/09/03
 * Time: 14:58
 */

namespace business\datacenter\repositories;

use business\datacenter\models\LandConstructionModel;
use business\datacenter\models\LandDataRsyncToEsCronRecordModel;
use business\datacenter\models\LandDataRsyncToEsErrorLogModel;
use business\datacenter\models\LandProjectModel;
use business\datacenter\models\LandTakeModel;
use yii\db\Query;

class LandRepository
{
    /**
     * 返回大数据拓客--拿地数据单个记录
     * @param array $condition
     * @param string $fields
     * @param array $orderBy
     * @return array|false
     * @throws \yii\db\Exception
     */
    public function getLandTakeOne(
        $condition = [],
        $fields = '*',
        $orderBy = ['created_time' => SORT_DESC]
    ) {
        return (new Query())
            ->from(LandTakeModel::tableName())
            ->select($fields)
            ->where($condition)
            ->orderBy($orderBy)
            ->createCommand(LandTakeModel::getDb())
            ->queryOne();
    }

    /**
     * 返回大数据拓客--开工数据单个记录
     * @param array $condition
     * @param string $fields
     * @param array $orderBy
     * @return array|false
     * @throws \yii\db\Exception
     */
    public function getLandConstructionOne(
        $condition = [],
        $fields = '*',
        $orderBy = ['created_time' => SORT_DESC]
    ) {
        return (new Query())
            ->from(LandConstructionModel::tableName())
            ->select($fields)
            ->where($condition)
            ->orderBy($orderBy)
            ->createCommand(LandConstructionModel::getDb())
            ->queryOne();
    }

    /**
     * 返回大数据拓客--项目数据单个记录
     * @param array $condition
     * @param string $fields
     * @param array $orderBy
     * @return array|false
     * @throws \yii\db\Exception
     */
    public function getLandProjectOne(
        $condition = [],
        $fields = '*',
        $orderBy = ['created_time' => SORT_DESC]
    ) {
        return (new Query())
            ->from(LandProjectModel::tableName())
            ->select($fields)
            ->where($condition)
            ->orderBy($orderBy)
            ->createCommand(LandProjectModel::getDb())
            ->queryOne();
    }

    /**
     * 返回大数据拓客--拿地数据列表
     * @param array $condition
     * @param string $fields
     * @param int $offset
     * @param int $pageSize
     * @param array $orderBy
     * @return array
     * @throws \yii\db\Exception
     */
    public function getLandTakeList(
        $condition = [],
        $starTime = 0,
        $endTime = 0,
        $fields = '*',
        $offset = 0,
        $pageSize = 15,
        $orderBy = ['updated_time' => SORT_DESC]
    ) {
        $query = (new Query())
            ->from(LandTakeModel::tableName())
            ->select($fields)
            ->where($condition);

        (!empty($starTime)) && $query->andWhere(['>=', 'updated_time', $starTime]);
        (!empty($endTime))  && $query->andWhere(['<=', 'updated_time', $endTime]);

        return $query->offset($offset)
            ->limit($pageSize)
            ->orderBy($orderBy)
            ->createCommand(LandTakeModel::getDb())
            ->queryAll();
    }

    /**
     * 返回大数据拓客--开工数据列表
     * @param array $condition
     * @param int $starTime
     * @param int $endTime
     * @param string $fields
     * @param int $offset
     * @param int $pageSize
     * @param array $orderBy
     * @return array
     * @throws \yii\db\Exception
     */
    public function getLandConstructionList(
        $condition = [],
        $starTime = 0,
        $endTime = 0,
        $fields = '*',
        $offset = 0,
        $pageSize = 15,
        $orderBy = ['updated_time' => SORT_DESC]
    ) {
        $query = (new Query())
            ->from(LandConstructionModel::tableName())
            ->select($fields)
            ->where($condition);

        (!empty($starTime)) && $query->andWhere(['>=', 'updated_time', $starTime]);
        (!empty($endTime))  && $query->andWhere(['<=', 'updated_time', $endTime]);

        return $query->offset($offset)
            ->limit($pageSize)
            ->orderBy($orderBy)
            ->createCommand(LandConstructionModel::getDb())
            ->queryAll();
    }

    /**
     * 返回大数据拓客--项目数据列表
     * @param array $condition
     * @param int $starTime
     * @param int $endTime
     * @param string $fields
     * @param int $offset
     * @param int $pageSize
     * @param array $orderBy
     * @return array
     * @throws \yii\db\Exception
     */
    public function getLandProjectList(
        $condition = [],
        $starTime = 0,
        $endTime = 0,
        $fields = '*',
        $offset = 0,
        $pageSize = 15,
        $orderBy = ['updated_time' => SORT_DESC]
    ) {
        $query = (new Query())
            ->from(LandProjectModel::tableName())
            ->select($fields)
            ->where($condition);

        (!empty($starTime)) && $query->andWhere(['>=', 'updated_time', $starTime]);
        (!empty($endTime))  && $query->andWhere(['<=', 'updated_time', $endTime]);

        return $query->offset($offset)
            ->limit($pageSize)
            ->orderBy($orderBy)
            ->createCommand(LandProjectModel::getDb())
            ->queryAll();
    }

    /**
     * 插入土地数据、开工数据、项目数据全量/增量同步到es调度执行记录表
     * @param array $data
     * @return array|int
     * @throws \yii\db\Exception
     */
    public function storeLandDataRsyncToEsCronRecord($data = [])
    {
        if (!empty($data)) {
            return (new Query())
                ->createCommand(LandDataRsyncToEsCronRecordModel::getDb())
                ->insert(LandDataRsyncToEsCronRecordModel::tableName(), $data)
                ->execute();
        }

        return [];
    }

    /**
     * 插入土地数据、开工数据、项目数据全量/增量同步到es中错误日志记录
     * @param array $data
     * @return array|int
     * @throws \yii\db\Exception
     */
    public function storeLandDataRsyncToEsErrorLog($data = [])
    {
        if (!empty($data)) {
            return (new Query())
                ->createCommand(LandDataRsyncToEsErrorLogModel::getDb())
                ->insert(LandDataRsyncToEsErrorLogModel::tableName(), $data)
                ->execute();
        }

        return [];
    }

    /**
     * 返回土地数据、开工数据、项目数据全量/增量同步到es调度执行记录
     * @param array $condition
     * @param string $fields
     * @param array $orderBy
     * @return array|false
     * @throws \yii\db\Exception
     */
    public function getLandDataRsyncToEsCronRecord(
        $condition = [],
        $fields = '*',
        $orderBy =
        ['created_time' => SORT_DESC]
    ) {
        return (new Query())
            ->from(LandDataRsyncToEsCronRecordModel::tableName())
            ->select($fields)
            ->where($condition)
            ->orderBy($orderBy)
            ->createCommand(LandDataRsyncToEsCronRecordModel::getDb())
            ->queryOne();
    }

    public function getYcHundredSupplierData($condition = [], $fields = '*')
    {
        return (new Query())
            ->from("b2b_data_center.youcai_hundred_supplier_data")
            ->select($fields)
            ->where($condition)
            ->createCommand(\Yii::$app->db)
            ->queryAll();
    }
}
